import sys
import ctypes
from ctypes import wintypes

WM_COPYDATA = 0x4a

context64bit = sys.maxsize > 2**32

if context64bit:
  class COPYDATASTRUCT(ctypes.Structure):
    _fields_ = [('dwData', ctypes.c_ulonglong),
      ('cbData', ctypes.wintypes.DWORD),
      ('lpData', ctypes.c_void_p)]
else:
  class COPYDATASTRUCT(ctypes.Structure):
    _fields_ = [('dwData', ctypes.wintypes.DWORD),
      ('cbData', ctypes.wintypes.DWORD),
      ('lpData', ctypes.c_void_p)]

def findWindow(title):
  hwnd = ctypes.windll.user32.FindWindowA(0, title.encode())
  return hwnd or None

def sendMessage(message, hwnd, dwData=10):
  assert isinstance(message, str)  
  sender_hwnd = 0
  buf = ctypes.create_string_buffer(message.encode())
  copydata = COPYDATASTRUCT()
  copydata.dwData = dwData
  copydata.cbData = buf._length_
  copydata.lpData = ctypes.cast(buf, ctypes.c_void_p)
  return ctypes.windll.user32.SendMessageA(
    hwnd, WM_COPYDATA, sender_hwnd,
    ctypes.byref(copydata))

if __name__ == '__main__':
  hwnd = findWindow('jmodebugger')
  print(hwnd)
  message = "q att"
  print(sendMessage(message,hwnd))
